package com.efast.dmom.mgmt.utils;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import com.efast.cafe.util.ConfigUtil;
import net.sf.jasperreports.engine.JRAbstractExporter;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.export.JRPdfExporterParameter;
import net.sf.jasperreports.engine.util.JRLoader;

public class PrintCardUtil<T> {

	private List<T> reportList;

	
	
	public List<T> getReportList() {
		return reportList;
	}



	public void setReportList(List<T> reportList) {
		this.reportList = reportList;
	}

    

	public PrintCardUtil(List<T> reportList) {
		this.reportList = reportList;
	}



	/**
	 * iReport集成web打印
	 * @param reportList 数据源，通过$F{key_name}获取
	 * @param parameters 参数，ireport中通过$P{key_name}获取
	 * @param file 需要导入的iReport模板文件
	 */
	public void iReportWebPrint(Map<String, Object> parameters, String filePath) {
		//工艺流程卡打印：得到模板文件实际地址
		String xmlPath = Thread.currentThread().getContextClassLoader().getResource(filePath).getPath();
		JasperPrint jasperPrint;
		try {
			ByteArrayOutputStream outPut=new ByteArrayOutputStream();  
	        FileOutputStream outputStream=null;  
	        File file=new File("C:"+ConfigUtil.getUploadDir()+"report.pdf");
	        if ( !file.exists()){//若此目录不存在，则创建之// 这个东西只能简历一级文件夹，两级是无法建立的。。。。。
	        	file.mkdir();
	        }
			if(reportList!=null&&reportList.size()>0) {
				//封装reportList中数据作为数据源 通过$F{key_name}获取
				JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(reportList);
				jasperPrint = JasperFillManager.fillReport(xmlPath, parameters, ds);
			}else {
				JREmptyDataSource ds = new JREmptyDataSource();
				jasperPrint = JasperFillManager.fillReport(xmlPath, parameters,ds);
			}
			//替换掉导出的代码，true为弹出打印机设置，false为直接打印  
			JRAbstractExporter exporter=new JRPdfExporter();  
            //创建jasperPrint  
            exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);  
            //生成输出流  
            exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outPut);  
            //屏蔽copy功能  
            exporter.setParameter(JRPdfExporterParameter.IS_ENCRYPTED,Boolean.TRUE);  
            //加密  
            exporter.setParameter(JRPdfExporterParameter.IS_128_BIT_KEY,Boolean.TRUE);  
            exporter.exportReport();  
            outputStream=new FileOutputStream(file);  
            outputStream.write(outPut.toByteArray()); 
            FTPclient ftpClient = new FTPclient() ;
            ftpClient.sendLayoutToPrinter("w");
//			JasperPrintManager.printReport(jasperPrint,false);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	

	/**
	 * iReport集成web打印
	 * @param reportList 数据源，通过$F{key_name}获取
	 * @param parameters 参数，ireport中通过$P{key_name}获取
	 * @param file 需要导入的iReport模板文件
	 */
	@SuppressWarnings("resource")
	public void iReportWebPrintResponse(Map<String, Object> parameters, String filePath,HttpServletResponse response) {
		//工艺流程卡打印：得到模板文件实际地址
		String xmlPath = Thread.currentThread().getContextClassLoader().getResource(filePath).getPath();
		try {
			xmlPath = URLDecoder.decode(xmlPath, "UTF-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		JasperPrint jasperPrint;
		try {
			response.setContentType("application/pdf");
			OutputStream out = response.getOutputStream();
			ByteArrayOutputStream outPut=new ByteArrayOutputStream();  
	        FileOutputStream outputStream=null; 
	       // File file=new File(ConfigUtil.getUploadDir()+"report.pdf");
	        File file001=new File("C://loader");
	        if ( !file001.exists()){//若此目录不存在，则创建之// 这个东西只能简历一级文件夹，两级是无法建立的。。。。。
	        	file001.mkdir();
	        }
	        File file=new File("C://loader//report.pdf");
			if(reportList!=null&&reportList.size()>0) {
				//封装reportList中数据作为数据源 通过$F{key_name}获取
				JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(reportList);
				FileInputStream fis = new FileInputStream(xmlPath);
				BufferedInputStream bufferedInputStream = new BufferedInputStream(fis);
				jasperPrint = JasperFillManager.fillReport((JasperReport)JRLoader.loadObject(bufferedInputStream), parameters, ds);
			}else {
				FileInputStream fis = new FileInputStream(xmlPath);
				BufferedInputStream bufferedInputStream = new BufferedInputStream(fis);
				JREmptyDataSource ds = new JREmptyDataSource();
				jasperPrint = JasperFillManager.fillReport((JasperReport)JRLoader.loadObject(bufferedInputStream), parameters,ds);
				// jasperPrint = JasperFillManager.fillReport(xmlPath, parameters,ds);
			}
			//替换掉导出的代码，true为弹出打印机设置，false为直接打印  
			JRAbstractExporter exporter=new JRPdfExporter();  
            //创建jasperPrint  
            exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);  
            //生成输出流  
            exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outPut);  
            //屏蔽copy功能  
            exporter.setParameter(JRPdfExporterParameter.IS_ENCRYPTED,Boolean.TRUE);  
            //加密  
            exporter.setParameter(JRPdfExporterParameter.IS_128_BIT_KEY,Boolean.TRUE);  
            exporter.exportReport();  
            outputStream=new FileOutputStream(file);  
            outputStream.write(outPut.toByteArray()); 
            FileInputStream in = new FileInputStream(file);
//            FTPclient ftpClient = new FTPclient() ;
//            ftpClient.sendLayoutToPrinter("w");
//			JasperPrintManager.printReport(jasperPrint,false);
            byte[] b = new byte[512];
            while ((in.read(b))!=-1) {
            out.write(b);
            }
            out.flush();
            in.close();
            out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
